// pages/sceneTrainReport/index.js
const api = require('../../config/api')
const request = require('../../utils/request')
const { echarts } = requirePlugin('echarts')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    reportData: {},
    age: 2,
    month: 3,
    option: {},
    optionList: [],
    trainingTaskDetail: {},
    trainingTaskMonthDetail: [],
    posterSrc:
      'https://kj-zhiliao.oss-cn-qingdao.aliyuncs.com/zl-backend3112705936ec4679ad331a5d1cac2faf.png', // 海报图片
    showPoster: false,
    shareInfo: {},
    modalHeight: app.kBottomSafeHeight + 345,
    bottomHeight: app.kBottomSafeHeight + 43,
		stepHeight: app.kBottomSafeHeight + 266,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getReportData(options.trainingId)
    this.getShareInfo(options.trainingId)
  },

  async getReportData(trainingId) {
    const res = await request.get(api.getReportDetail, { trainingId })
    if (res.httpStatus === 200) {
      this.setData({
        reportData: res.data,
        trainingTaskDetail: res.data.trainingTaskDetail,
        trainingTaskMonthDetail:
          res.data.trainingTaskDetail.trainingTaskMonthDetail,
      })

      let { optionList } = this.data
      res.data.trendChartDataList.map((obj) => {
        this.getChartData(obj, optionList)
      })

      this.setData({
        optionList,
      })
    }
  },

  getChartData: function (obj, optionList) {
    let info = wx.getSystemInfoSync().system
    let isios = info.indexOf('ios') != -1

    let list = obj.dayDetailList
    const Ydata = list.map((obj) => {
      return obj.score
    })
    const Xdata = list.map((obj) => {
      return obj.taskTime.substring(5, 10)
    })

    const option = {
      grid: {
        left: isios ? '12%' : '10%',
        right: isios ? '12%' : '10%',
        height: '75%',
        top: 10,
        bottom: 20,
      },
      xAxis: {
        boundaryGap: false,
        axisLabel: {
          showMaxLabel: true,
          color: '#5D5C5D',
        },
        axisLine: {
          lineStyle: {
            color: '#bebebe',
            type: 'dashed',
          },
        },
        data: Xdata,
      },
      yAxis: {
        splitLine: {
          show: true,
          lineStyle: {
            type: 'dashed',
            color: '#bebebe',
          },
        },
        axisLabel: {
          showMaxLabel: true,
          color: '#5D5C5D',
        },
      },
      series: [
        {
          data: Ydata,
          type: 'line',
          smooth: true,
          symbolSize: 0,
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
              {
                offset: 1,
                color: 'rgba(0,203,106,0.60)',
              },
              {
                offset: 0,
                color: 'rgba(0,203,106,0.10)',
              },
            ]),
          },
          itemStyle: {
            normal: {
              color: '#00CB6A', //改变折线点的颜色
              borderWidth: 30,
              lineStyle: {
                color: '#00CB6A', //改变折线颜色
              },
            },
          },
        },
      ],
      tooltip: {
        trigger: 'axis',
        borderColor: '#efbe24',
        backgroundColor: '#efbe24',
        formatter: '{c0}分',
        textStyle: {
          color: '#fff',
          fontSize: '18',
        },
        padding: [5, 5, 5, 5],
      },
    }

    let item = {
      questionnaireName: obj.questionnaireName,
      firstIncrease: obj.firstIncrease,
      option: option,
    }
    optionList.push(item)
  },

  async getShareInfo(trainingId) {
    const res = await request.get(api.shareTrainInfo + trainingId)
    if (res.httpStatus === 200) {
      this.setData({
        shareInfo: res.data,
      })

      if (res.data.posterImageUrl) {
        this.setData({
          posterSrc: res.data.posterImageUrl,
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.shareSheet = this.selectComponent('#shareSheet')
  },
  // 显示分享弹窗
  showShareSheet() {
    this.setData({
      showPoster: true,
    })
    this.shareSheet.show()
  },
  // 隐藏分享弹窗
  hideShareSheet() {
    this.setData({
      showPoster: false,
    })
    this.shareSheet.hide()
  },
  // 保存图片到相册
  savePic: function () {
    let that = this
    let { posterSrc } = this.data
    wx.getImageInfo({
      src: posterSrc,
      success: function (sres) {
        wx.saveImageToPhotosAlbum({
          filePath: sres.path,
          success: function (fres) {
            that.hideShareSheet()
            wx.showToast({
              title: '保存成功',
              icon: 'success',
            })
          },
          fail: function (fres) {
            wx.showToast({
              title: '保存失败',
              icon: 'error',
            })
          },
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let { shareInfo } = this.data
    return {
      title: shareInfo.trainingName,
      path: '/pages/sceneTrainDetail/index?trainingId=' + shareInfo.trainingId,
      imageUrl:
        shareInfo.shareImageUrl ||
        'https://kj-zhiliao.oss-cn-qingdao.aliyuncs.com/zl-backend5d6c29cf96f348d08c6a56d83a995686.png',
    }
  },
})
